package cn.ifavor.appframework.recyclerview;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;

import java.util.List;

import cn.ifavor.appframework.BaseActivity;
import cn.ifavor.appframework.R;
import cn.ifavor.appframework.lifecycle.bean.User;

/**
 * @author: SvenHe(heshiweij@gmail.com)
 * @Date: 2016-06-05
 * @Time: 16:07
 * @des ${TODO}
 */
public abstract class BaseRecyclerActivity<T> extends BaseActivity  implements SwipeRefreshLayout.OnRefreshListener {

    protected RecyclerView mRecyclerView;
    protected List<T> mDatas;
    protected SwipeRefreshLayout mSwipeRefreshLayout;
    protected MyListAdapter mListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 初始化内容视图
        setUpContentView();

        // 初始化内容
        setUpView();

        // 初始化数据
        setUpData();

        mRecyclerView.setLayoutManager(getLayoutManager());
        mListAdapter = new MyListAdapter(this, mDatas);
        mRecyclerView.setAdapter(mListAdapter);
    }

    /**
     * 初始化内容视图
     */
    protected abstract void setUpContentView();

    /**
     * 初始化数据
     */
    protected abstract List<User> setUpData();

    /**
     * 获取布局管理器（默认线性布局）
     * @return
     */
    protected RecyclerView.LayoutManager getLayoutManager(){
        return new LinearLayoutManager(this);
    }

    /**
     * 初始化内容
     */
    protected void setUpView() {
        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_layout);
        mSwipeRefreshLayout.setOnRefreshListener(this);
    }

    @Override
    public abstract void onRefresh();

    /**
     * 适配器
     */
    public class MyListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        private final Context mContext;
        List<T> mUsers;

        public MyListAdapter(Context context, List<T> users) {
            mUsers = users;
            mContext = context;
        }

        /**
         * 当条目创建时调用
         *
         * @param parent
         * @param viewType
         * @return
         */
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return getViewHolder(parent, viewType);
        }

        /**
         * 当条目绑定时调用
         *
         * @param holder
         * @param position
         */
        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            onBind(holder, position);
        }

        /**
         * 获取当前条数
         *
         * @return
         */
        @Override
        public int getItemCount() {
            return mUsers == null ? 0 : mUsers.size();
        }

    }

    protected abstract void onBind(RecyclerView.ViewHolder holder, int position);

    protected abstract RecyclerView.ViewHolder getViewHolder(ViewGroup parent, int viewType);

}
